import React, { Component } from 'react'
import './count.css'

export default class Count extends Component {
  state = {
    count: 0,
  }

  increament = () => {
    const { value } = this.selectRef
    const { count } = this.state
    this.setState({ count: count + value*1 })
  }

  decrement = () => {
    const { value } = this.selectRef
    const { count } = this.state
    this.setState({ count: count - value*1 })
  }

  increamentIfOdd = () => {
    const { value } = this.selectRef
    const { count } = this.state
    if (count % 2) {
      this.setState({ count: count + value*1 })
    }
  }

  increamentAsync = () => {
    const { value } = this.selectRef
    const { count } = this.state
    setTimeout(() => {
      this.setState({ count: count + value*1 })
    }, 1000)
  }

  render() {
    const { count } = this.state
    return (
      <div className="count">
        <h1>当前求和为: { count }</h1>
        <select ref={c => (this.selectRef = c)}>
          <option value="1">1</option>
          <option value="2">2</option>
          <option value="3">3</option>
        </select>
        <div className="btns">
          <button onClick={this.increament}>+</button>
          <button onClick={this.decrement}>-</button>
          <button onClick={this.increamentIfOdd}>当前求和为奇数再加</button>
          <button onClick={this.increamentAsync}>异步加</button>
        </div>
      </div>
    )
  }
}
